#include <glib.h>

static void _queue_foreach_func(gpointer data, gpointer       user_data)
{
    g_print("user_data:%s, data:%d \n", (gchar *)user_data, GPOINTER_TO_INT(data));
}

static gint _queue_find_cmp_func(gconstpointer a, gconstpointer b)
{
    gint x, y;

    x = GPOINTER_TO_INT(a);
    y = GPOINTER_TO_INT(b);

    return (x > y ? +1 : x == y ? 0 : -1);
}

gint main(gint argc, gchar **argv)
{
    GQueue *q = NULL;
    gint index1 = 0, index2 = 0;
    GList *l = NULL;
    GList *l1 = NULL, *l2 = NULL;

    q = g_queue_new();

    g_queue_push_tail(q, GINT_TO_POINTER(10));
    g_queue_push_tail(q, GINT_TO_POINTER(20));
    g_queue_push_tail(q, GINT_TO_POINTER(30));
    g_queue_push_tail(q, GINT_TO_POINTER(40));
    // result: 10-20-30-40

    g_queue_foreach(q, _queue_foreach_func, "Queue-push");

    // find pos
    index1 = g_queue_index(q, GINT_TO_POINTER(40));

    l = g_new0(GList, 1);
    l->data = GINT_TO_POINTER(30);
    index2 = g_queue_link_index(q, l);
    g_print("index1:%d, index2:%d \n", index1, index2);

    // find GList
    l1 = g_queue_find(q, GINT_TO_POINTER(20));
    if(NULL != l1) {
        g_print("l1->data:%d \n", GPOINTER_TO_INT(l1->data));
    } else {
        g_print("g_queue_find not found \n");
    }
    l2 = g_queue_find_custom(q, GINT_TO_POINTER(10), _queue_find_cmp_func);
    if(NULL != l2) {
        g_print("l2->data:%d \n", GPOINTER_TO_INT(l2->data));
    } else {
        g_print("g_queue_find_custom not found \n");
    }

    g_queue_foreach(q, _queue_foreach_func, "Queue-after-find");

    g_list_free(l);
    g_queue_free(q);

    return 0;
}